﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace GISforTT_API
{
    /// <summary>
    /// Interaction logic for CornerMenu.xaml
    /// </summary>
    public partial class CornerMenu : UserControl
    {
        /// <summary>
        /// location of the corner menu
        /// </summary>
        public enum CornerMenuLocation
        {
            TopLeft = 1,
            TopRight = 2,
            BottomLeft = 3,
            BottomRight = 4,
        }

        #region members
        
        private DispatcherTimer timer;
        private int ShrinkTime;

        private CornerMenuLocation location;
        public CornerMenuLocation Location
        {
            get
            {
                return location;
            }

            set
            {
                location = value;
            }
        }

        private bool BkListBtnAdded = false;
        private bool LayersMgrBtnAdded = false;
        private bool SearchBtnAdded = false;
        private bool ExitBtnAdded = false;

        #endregion members

        /// <summary>
        /// Constructor of the CornerMenu
        /// </summary>
        /// <param name="menuLocation">Represents which corner of the screen this menu will be assigned</param>
        public CornerMenu(CornerMenuLocation menuLocation)
        {
            InitializeComponent();

            location = menuLocation;

            CreateFunctionButtons();

            InitializeTimer();
        }

        /// <summary>
        /// Creates the Buttons on the corner menu
        /// </summary>
        public void CreateFunctionButtons()
        {
            // TODO: only 4 buttons allowed for now - until the rotating dial is added
            int count = 0;

            List<CornerMenusMgr.CornerMenuFunction> menuFunctions = CornerMenusMgr.Instance.MenuFunctions;

            foreach (CornerMenusMgr.CornerMenuFunction func in menuFunctions)
            {
                // Only 4 for now
                if (count > 3) break;


                Ellipse button = new Ellipse();

                switch (func)
                {
                    case CornerMenusMgr.CornerMenuFunction.LayersMgrFunction:
                        button.Name = "ConfigButton";
                        button.Fill = Resources["Config"] as ImageBrush;
                        button.Width = 30;
                        button.Height = 30;
                        button.MouseLeftButtonDown += ConfigButton_Click;
                        ButtonsGrid.Children.Add(button);
                        
                        count++;
                        LayersMgrBtnAdded = true;
                        break;

                    //case CornerMenusMgr.CornerMenuFunction.SearchFunction:
                    //    // Under development
                    //    button.Name = "SearchButton";
                    //    button.Fill = Resources["Find"] as ImageBrush;
                    //    button.Width = 30;
                    //    button.Height = 30;
                    //    button.MouseLeftButtonDown += SearchButton_Click;
                    //    ButtonsGrid.Children.Add(button);

                    //    count++;
                    //    SearchBtnAdded = true;

                    //    break;
                    case CornerMenusMgr.CornerMenuFunction.BookmarkListFunction:
                        button.Name = "ListButton";
                        button.Fill = Resources["List"] as ImageBrush;
                        button.Width = 30;
                        button.Height = 30;
                        button.MouseLeftButtonDown += ListButton_Click;
                        ButtonsGrid.Children.Add(button);
                        
                        count++;
                        BkListBtnAdded = true;

                        break;
                    case CornerMenusMgr.CornerMenuFunction.ExitFunction:
                        button.Name = "ExitButton";
                        button.Fill = Resources["Exit"] as ImageBrush;
                        button.Width = 30;
                        button.Height = 30;
                        button.MouseLeftButtonDown += ExitButton_Click;
                        ButtonsGrid.Children.Add(button);
                                                
                        count++;
                        ExitBtnAdded = true;

                        break;
                    case CornerMenusMgr.CornerMenuFunction.CustomFunction:
                        // TODO-VAR: custom functions
                        break;
                }

                // Set the position of the button
                if (count == 1)
                {
                    Grid.SetRow(button, 3);
                }
                else if (count == 2)
                {
                    button.Margin = new Thickness(20, 24, 10, 6);
                    Grid.SetRow(button, 1);
                    Grid.SetColumnSpan(button, 2);
                    Grid.SetRowSpan(button, 2);                        
                }
                else if (count == 3)
                {
                    button.Margin = new Thickness(20, 22, 10, 8);
                    Grid.SetColumn(button, 1);
                    Grid.SetColumnSpan(button, 2);
                    Grid.SetRowSpan(button, 2);
                }
                else
                {
                    Grid.SetColumn(button, 3);
                }
            }

            // TODO: need to make sure that the exit function is added
        }

        
        #region event handlers

        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            timer.Stop();
            CornerMenusMgr.Instance.showExitMenu(0.0 /*FIX ME: orientation*/, location);
        }

        private void ListButton_Click(object sender, RoutedEventArgs e)
        {
            CornerMenusMgr.Instance.showListBox(0.0 /*FIX ME: orientation*/, location);
        }

        private void ConfigButton_Click(object sender, RoutedEventArgs e)
        {
            CornerMenusMgr.Instance.ShowMapLayersManagerPanel(location);
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            CornerMenusMgr.Instance.viewSearchDialog();
        }

        private void CollapsedMenu_Click(object sender, RoutedEventArgs e)
        {
            ToggleMenuState();
        }

        private void ExpandedMenuEllipse_Click(object sender, RoutedEventArgs e)
        {
            ToggleMenuState();
        }

        private void ToggleMenuState()
        {
            if (ExpandedMenu.Visibility == Visibility.Hidden)
            {
                // Expanded menu is hidden
                timer.Start();

                CollapsedMenu.Visibility = Visibility.Hidden;
                ExpandedMenu.Visibility = Visibility.Visible;
                                
                Grid.SetZIndex(ExpandedMenu, 2);
                Grid.SetZIndex(CollapsedMenu, 1);
            }
            else
            {
                // Expanded menu is visible
                if (!CornerMenusMgr.Instance.isExitDialogVisible())
                {
                    ExpandedMenu.Visibility = Visibility.Hidden;
                    CollapsedMenu.Visibility = Visibility.Visible;

                    Grid.SetZIndex(ExpandedMenu, 1);
                    Grid.SetZIndex(CollapsedMenu, 2);

                    timer.Stop();
                }
            }
        }
        
        #endregion

        #region animations

        /// <summary>
        /// Initialize the timer of the corner menu
        /// </summary>
        public void InitializeTimer()
        {
            this.ShrinkTime = 5;
            timer = new System.Windows.Threading.DispatcherTimer();
            timer.Tick += new EventHandler(dTimer_Tick);
            timer.Interval = new TimeSpan(0, 0, ShrinkTime);
        }

        /// <summary>
        /// Starts the timer of the corner menu
        /// </summary>
        public void startTimer()
        {
            timer.Start();
        }

        private void dTimer_Tick(object sender, EventArgs args)
        {
            if (((DispatcherTimer)sender == timer))
            {
                ExpandedMenu.Visibility = Visibility.Hidden;
                CollapsedMenu.Visibility = Visibility.Visible;
            }
        }
        #endregion

    }
}
